﻿using AutoMapper;
using MediatR;
using SU.Center.Domain.RBAC;
using SU.Center.Infa;

namespace SU.Center.Api.Handler.RBAC
{
    /// <summary>
    /// 显示角色权限
    /// </summary>
    public class ShowRolePermissionsHandler : IRequestHandler<ShowRolePermissionsRepsultDto, ApiResult>
    {
        private readonly IBaseReposiotry<RolePermissions> _rolePermissionsRepository;
        private readonly ILogger<ShowRolePermissionsHandler> _logger;

        public ShowRolePermissionsHandler(IBaseReposiotry<RolePermissions> rolePermissionsRepository,
            ILogger<ShowRolePermissionsHandler> logger)
        {
            _rolePermissionsRepository = rolePermissionsRepository;
            _logger = logger;
        }

        public async Task<ApiResult> Handle(ShowRolePermissionsRepsultDto request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _rolePermissionsRepository.GetAll();
                return await Task.FromResult(ApiResult.Ok(list));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "ShowRolePermissionsHandler");
                throw;
            }
        }
    }
}

public class ShowRolePermissionsRepsultDto : IRequest<ApiResult>
{
    ///角色Id
    /// </summary>
    public int RoleId { get; set; }

    /// <summary>
    /// 权限Id
    /// </summary>
    public int PermissionId { get; set; }
}
